package com.nd.teamfile.fileexplorer.helper;

import java.util.Comparator;
import java.util.HashMap;

import com.nd.teamfile.fileexplorer.sdk.type.FileExplorerInfo;
import com.nd.teamfile.fileexplorer.util.FileExplorerUtil;

/**
 * 文件排序类
 * 
 * @ClassName FileSortHelper
 * @Function TODO ADD FUNCTION
 * @Reason TODO ADD REASON
 * 
 * @author HuangYK
 * @version v1.3.3
 * @Date 2014-1-24 下午7:23:02
 * 
 * @see
 */
public class FileSortHelper {
	/**
	 * 排序方式(name:文件名, size:文件长度, date:修改时间, type:文件类型)
	 * 
	 * @ClassName SortMethod
	 * @Function TODO ADD FUNCTION
	 * @Reason TODO ADD REASON
	 * 
	 * @author HuangYK
	 * @version v1.3.3
	 * @Date 2014-1-24 下午7:32:42
	 * 
	 * @see
	 */
	public enum SortMethod {
		name, size, date, type
	}

	// 默认文件名排序
	private SortMethod mSort = SortMethod.name;

	private HashMap<SortMethod, Comparator<FileExplorerInfo>> mComparatorList = new HashMap<SortMethod, Comparator<FileExplorerInfo>>();

	public FileSortHelper() {
		mComparatorList.put(SortMethod.name, cmpName);
		mComparatorList.put(SortMethod.size, cmpSize);
		mComparatorList.put(SortMethod.date, cmpDate);
		mComparatorList.put(SortMethod.type, cmpType);
	}

	/**
	 * 设置排序方式
	 * 
	 * @n<b>函数名称</b> :setSortMethog
	 * @brief
	 * @see
	 * @version Ver 1.3.x
	 * @param @param s
	 * @return void
	 * @<b>作者</b> : HuangYK
	 * @<b>创建时间</b> : 2014-1-27下午5:39:55
	 */
	public void setSortMethog(SortMethod s) {
		mSort = s;
	}

	public Comparator<? super FileExplorerInfo> getComparator() {
		return mComparatorList.get(mSort);
	}

	private abstract class FileComparator implements
			Comparator<FileExplorerInfo> {

		@Override
		public int compare(FileExplorerInfo object1, FileExplorerInfo object2) {
			// 同类型的文件，则进行比较
			if (object1.IsDir == object2.IsDir) {
				return doCompare(object1, object2);
			}
			// 文件夹放前面，文件放后面
			return object1.IsDir ? -1 : 1;
		}

		protected abstract int doCompare(FileExplorerInfo object1,
				FileExplorerInfo object2);
	}

	/**
	 * 获取按文件名排序列表
	 */
	private Comparator<FileExplorerInfo> cmpName = new FileComparator() {
		@Override
		public int doCompare(FileExplorerInfo object1, FileExplorerInfo object2) {
			return object1.fileName.compareToIgnoreCase(object2.fileName);
		}
	};

	/**
	 * 获取按文件大小排序列表
	 */
	private Comparator<FileExplorerInfo> cmpSize = new FileComparator() {
		@Override
		public int doCompare(FileExplorerInfo object1, FileExplorerInfo object2) {
			return longToCompareInt(object1.fileSize - object2.fileSize);
		}
	};
	/**
	 * 获取按修改时间排序列表
	 */
	private Comparator<FileExplorerInfo> cmpDate = new FileComparator() {
		@Override
		public int doCompare(FileExplorerInfo object1, FileExplorerInfo object2) {
			return longToCompareInt(object2.ModifiedDate - object1.ModifiedDate);
		}
	};

	/**
	 * 获取按文件类型排序列表
	 */
	private Comparator<FileExplorerInfo> cmpType = new FileComparator() {
		@Override
		public int doCompare(FileExplorerInfo object1, FileExplorerInfo object2) {
			int result = FileExplorerUtil.getExtFromFilename(object1.fileName)
					.compareToIgnoreCase(
							FileExplorerUtil
									.getExtFromFilename(object2.fileName));
			if (result != 0)
				return result;

			return FileExplorerUtil.getNameFromFilename(object1.fileName)
					.compareToIgnoreCase(
							FileExplorerUtil
									.getNameFromFilename(object2.fileName));
		}
	};

	private int longToCompareInt(long result) {
		return result > 0 ? 1 : (result < 0 ? -1 : 0);
	}

}
